

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCombinerUtility {

	private boolean isFileEnded = false;
	private int numeroChunk;
	private static final int CHUNK_SIZE = 1000;
	private static final int EOF = 26;
	private static final int STARTING_OFFSET = 24;
	private String fileName = null;
	private FileOutputStream stream = null;
	
	FileCombinerUtility(String FileName) throws FileNotFoundException{
		fileName = new String(FileName);
		numeroChunk = 0;
		stream = new FileOutputStream(new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "Serveur_" +fileName), true);
	}
	
	public int getAckNumber(byte[] dataReceived) {
		return (int)dataReceived[0];
	}

	public void makeFile(byte[] receivedData) throws IOException {
		if(receivedData[1] == 0){
			for(int i = STARTING_OFFSET ; i < receivedData.length; i++){
				stream.write(receivedData[i]);
				stream.flush();
			}
		}
		else{
			int indexFin = ( ((receivedData[2] & 0xff) << 24) + ( (receivedData[3] & 0xff) << 16 ) + ( (receivedData[4] & 0xff)  << 8 ) + ( (receivedData[5]  & 0xff) ));
			for(int i = STARTING_OFFSET ; i < indexFin; i++){
				stream.write(receivedData[i]);
				stream.flush();
			}
		}
	}

	public byte[] getNextAck(int debutFenetre) {
		byte[] nextAck = new byte[1];
		nextAck[0] = (byte)debutFenetre;
		return nextAck;
	}

}
